<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>apache2服务器 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="title: 安装Apache2服务器author: John Doedate: 2022-01-11 21:06:21tags:Welcome to Avrilzion blog! This is your very first post.  安装Apache2服务器  使用以下命令在Linux1上安装Apache2服务器： 12sudo apt updatesudo apt install a">
<meta property="og:type" content="article">
<meta property="og:title" content="apache2服务器">
<meta property="og:url" content="http://example.com/posts/1001/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="title: 安装Apache2服务器author: John Doedate: 2022-01-11 21:06:21tags:Welcome to Avrilzion blog! This is your very first post.  安装Apache2服务器  使用以下命令在Linux1上安装Apache2服务器： 12sudo apt updatesudo apt install a">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-21T16:15:52.000Z">
<meta property="article:modified_time" content="2023-04-21T16:16:27.412Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-apache2服务器" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/posts/1001/" class="article-date">
  <time class="dt-published" datetime="2023-04-21T16:15:52.000Z" itemprop="datePublished">2023-04-22</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      apache2服务器
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="title-安装Apache2服务器author-John-Doedate-2022-01-11-21-06-21tags"><a href="#title-安装Apache2服务器author-John-Doedate-2022-01-11-21-06-21tags" class="headerlink" title="title: 安装Apache2服务器author: John Doedate: 2022-01-11 21:06:21tags:"></a>title: 安装Apache2服务器<br>author: John Doe<br>date: 2022-01-11 21:06:21<br>tags:</h2><p>Welcome to <a target="_blank" rel="noopener" href="https://avrilzion.github.io/ifyblog">Avrilzion blog</a>! This is your very first post.</p>
<ol>
<li>安装Apache2服务器</li>
</ol>
<p>使用以下命令在Linux1上安装Apache2服务器：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt update</span><br><span class="line">sudo apt install apache2</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>配置Apache2服务器</li>
</ol>
<p>打开Apache2的配置文件<code>/etc/apache2/sites-available/000-default.conf</code>，在<code>&lt;VirtualHost *:80&gt;</code>标签内添加以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ServerName www.skills.lan</span><br><span class="line">ServerAlias any.skills.lan</span><br><span class="line">RedirectMatch ^/(.*)$ https://www.skills.lan/$1</span><br></pre></td></tr></table></figure>

<p>该配置将任何使用<code>skills.lan</code>或<code>any.skills.lan</code>访问的请求自动重定向到<code>www.skills.lan</code>。</p>
<p>接着，打开Apache2的默认配置文件<code>/etc/apache2/sites-available/default-ssl.conf</code>，修改以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SSLCertificateFile /etc/ssl/apache.crt</span><br><span class="line">SSLCertificateKeyFile /etc/ssl/apache.key</span><br></pre></td></tr></table></figure>

<p>并添加以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SSLEngine on</span><br><span class="line">SSLVerifyClient require</span><br><span class="line">SSLVerifyDepth 10</span><br><span class="line">SSLCACertificateFile /etc/ssl/ca.crt</span><br></pre></td></tr></table></figure>

<p>这将启用SSL，要求客户端提供证书，并使用<code>/etc/ssl/ca.crt</code>中的证书作为可信CA证书。</p>
<p>最后，将<code>/var/www/html/index.html</code>文件替换为以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">	&lt;title&gt;apache&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">	&lt;h1&gt;Welcome to Apache!&lt;/h1&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>生成证书文件</li>
</ol>
<p>使用以下命令将<code>/etc/ssl/skills.crt</code>和<code>/etc/ssl/skills.key</code>转换为<code>/etc/ssl/skills.pfx</code>：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">openssl pkcs12 -export -out /etc/ssl/skills.pfx -inkey /etc/ssl/skills.key -in /etc/ssl/skills.crt</span><br></pre></td></tr></table></figure>

<p>接着，使用以下命令将<code>/etc/ssl/skills.pfx</code>转换为<code>/etc/ssl/skills.pem</code>：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">openssl pkcs12 -in /etc/ssl/skills.pfx -out /etc/ssl/skills.pem -nodes</span><br></pre></td></tr></table></figure>

<p>最后，使用以下命令从<code>/etc/ssl/skills.pem</code>提取证书和私钥，并分别保存到<code>/etc/ssl/apache.crt</code>和<code>/etc/ssl/apache.key</code>：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">openssl x509 -in /etc/ssl/skills.pem -out /etc/ssl/apache.crt</span><br><span class="line">openssl rsa -in /etc/ssl/skills.pem -out /etc/ssl/apache.key</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>重启Apache2服务</li>
</ol>
<p>使用以下命令重启Apache2服务：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo service apache2 restart</span><br></pre></td></tr></table></figure>

<p>现在，当使用<code>skills.lan</code>或<code>any.skills.lan</code>访问Linux1时，请求将自动重定向到<code>www.skills.lan</code>，且要求客户端提供SSL证书。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/posts/1001/" data-id="clgrhfks60000mts6bqoxadvd" data-title="apache2服务器" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/posts/1003/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">tomcat服务</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/04/">四月 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/posts/1001/">apache2服务器</a>
          </li>
        
          <li>
            <a href="/posts/1003/">tomcat服务</a>
          </li>
        
          <li>
            <a href="/posts/1003/">ansible服务</a>
          </li>
        
          <li>
            <a href="/posts/1002/">dns服务</a>
          </li>
        
          <li>
            <a href="/posts/1002/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>